package com.zhz.lib.project.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhz.lib.project.service.custom.CustomService;
import com.zhz.lib.project.vo.custom.CustomVO;
import com.zhz.project.common.util.alibaba.StringUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-2 下午10:54:39
 * 
 */
@Controller
@RequestMapping("/custom.do")
public class CustomController {
    private static String LINK    = "LINK";
    private static String FIND    = "find";
    private static String SERVICE = "service";
    private static String HELP    = "help";
    private static String ABOUT   = "about";
    @Autowired
    private CustomService customService;

    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET)
    public String get(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        String idString = request.getParameter("id");

        //如果是外部链接，则跳转到链接地址
        CustomVO match = customService.getCustomByCustomID(idString);
        if (StringUtil.equals(match.getKeyword(), LINK)) {
            String url = StringUtil.trim(match.getContent());
            return "redirect:" + url;
        }

        CustomVO voCustomVO = customService.getCustomByCustomIDWithSub(idString);
        if (voCustomVO != null) {
            CustomVO topCustomVO = null;
            CustomVO levelOneCustomVO = null;
            CustomVO levelTwoCustomVO = null;

            // 如果是顶级模块
            if (StringUtil.equals("0", voCustomVO.getLevel())) {
                topCustomVO = voCustomVO;
                levelOneCustomVO = customService.getFirstOfSubCustom(voCustomVO.getCustomId() + "");
                levelTwoCustomVO = customService.getFirstOfSubCustom(levelOneCustomVO.getCustomId()
                                                                     + "");
                if (levelOneCustomVO != null)
                    voCustomVO = levelOneCustomVO;

                if (levelTwoCustomVO != null)
                    voCustomVO = levelTwoCustomVO;
            }
            // 如果是一级模块
            else if (StringUtil.equals("1", voCustomVO.getLevel())) {
                levelOneCustomVO = voCustomVO;
                topCustomVO = customService.getCustomByCustomID(voCustomVO.getSortType());
                levelTwoCustomVO = customService.getFirstOfSubCustom(voCustomVO.getCustomId() + "");
            }
            // 如果是二级模块
            else if (StringUtil.equals("2", voCustomVO.getLevel())) {
                levelTwoCustomVO = voCustomVO;
                levelOneCustomVO = customService.getCustomByCustomID(voCustomVO.getSortType());
                topCustomVO = customService.getCustomByCustomID(levelOneCustomVO.getSortType());
            }

            //如果没有二级模块，则显示一级模块的内容
            if (levelTwoCustomVO == null) {
                voCustomVO = levelOneCustomVO;
            } else {
                voCustomVO = levelTwoCustomVO;
            }

            //如果用来显示的模块是外部链接，则跳转到链接地址
            if (StringUtil.equals(voCustomVO.getKeyword(), LINK)) {
                String url = StringUtil.trim(voCustomVO.getContent());
                return "redirect:" + url;
            }

            model.put("voCustomVO", voCustomVO);
            model.put("topCustomVO", topCustomVO);
            model.put("levelOneCustomVO", levelOneCustomVO);
            model.put("levelTwoCustomVO", levelTwoCustomVO);

            if (topCustomVO != null) {

                List<CustomVO> levelOneList = customService.getSubCustom(String.valueOf(topCustomVO
                    .getCustomId()));
                model.put("levelOneList", levelOneList);
            }

            if (levelOneCustomVO != null) {
                List<CustomVO> levelTwoList = customService.getSubCustom(String
                    .valueOf(levelOneCustomVO.getCustomId()));

                model.put("levelTwoList", levelTwoList);
                if (levelTwoList != null && levelTwoList.size() > 0)
                    model.put("levelTwoListShow", "levelTwoListShow");
            }

            customService.customHitPlusOne(Integer.parseInt(idString));

            if (topCustomVO != null) {
                if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(), FIND)) {
                    model.put("type", FIND);
                } else if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(), SERVICE)) {
                    model.put("type", SERVICE);
                } else if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(), HELP)) {
                    model.put("type", HELP);
                } else if (StringUtil.equalsIgnoreCase(topCustomVO.getKeyword(), ABOUT)) {
                    model.put("type", ABOUT);
                }
                return "customCommon";
            } else {
                return null;
            }
        }
        return null;
    }
}
